import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

public class ShowTweets extends JPanel implements TLModule{

	private final int LENGTH = 200;
	private final int HEIGHT = 250;
	private ArrayList<String> tweets;
	private ArrayList<String> usrs;
	private Long phraseID;
	private MyModel model;
	private JTable table;
	
	public ShowTweets(Framework fw_in){
		tweets = new ArrayList<String>();
		usrs = new ArrayList<String>();
	}
	
	public void setTable(){
		model = new MyModel();
		table = new JTable(){
			public Component prepareRenderer(TableCellRenderer renderer, int row, int col)
			{
				Component c = super.prepareRenderer(renderer, row, col);
				
				if((row % 2) == 1)
					c.setForeground(Color.YELLOW);
				else
					c.setForeground(Color.WHITE);

				return c;
			}
		};
		table.setModel(model);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setTableHeader(null);
		table.setShowGrid(false);
		table.setBackground(Color.BLACK);
		table.setForeground(Color.WHITE);
		String[] data = new String[2*tweets.size()];
		for (int i=0;i<data.length;i=i+2){
			data[i] = tweets.get(i/2);
			data[i+1] = usrs.get(i/2);
		}
		model.addColumn("one", data);
		int tableLength = 800;
		table.getColumnModel().getColumn(0).setMinWidth(tableLength);
		table.getColumnModel().getColumn(0).setMaxWidth(tableLength);
		table.getColumnModel().getColumn(0).setPreferredWidth(tableLength);

		DefaultTableCellRenderer dtc=new DefaultTableCellRenderer();   
        dtc.setForeground(java.awt.Color.YELLOW);   

        JScrollPane js = new JScrollPane(table);
		js.setPreferredSize(new Dimension(LENGTH,HEIGHT));
		this.add(js);
        
	}
	
	// connect database
	public void connectDatabase(){
		tweets = new ArrayList<String>();
		usrs = new ArrayList<String>();
		Connection connection = null;
        try {
        	// Load the JDBC driver
        	String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
        	Class.forName(driverName);

	        // Create a connection to the database
	        String serverName = "localhost";
	        String mydatabase = "infvis";
	        String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
	        String username = "root";
	        String password = "19851005";
	        connection = DriverManager.getConnection(url, username, password);
        	} 
        catch (ClassNotFoundException e) {
	        // Could not find the database driver
	        } 
        catch (SQLException e) {
	        // Could not connect to the database
	        } 
		QueryRunner qr = new QueryRunner();
		ResultSetHandler rsh = new MapListHandler();
		
		String sql1 = "SELECT from_user_id, tweet_text " +
				"FROM phrase_tweet " +
				"JOIN tweet " +
				"USING (tweet_id) " +
				"Where phrase_id = " + phraseID +
				" ORDER BY created_date DESC " +
				"LIMIT 0,20";
		
		try {
			List<Map> results = (List<Map>) qr.query(connection, sql1, rsh);
			for (Map row:results){
				tweets.add((String) row.get("tweet_text"));
				usrs.add((String) row.get("from_user_id"));
				}
			}
		catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	class MyModel extends DefaultTableModel{
		public boolean isCellEditable(int x, int y){
			return false;
		}
	}
	
	@Override
	public void highlightPhrase(List<Long> phraseId) {
		reset();
		phraseID = phraseId.get(phraseId.size()-1);
		connectDatabase();
		setTable();
		this.updateUI();
		
	}

	@Override
	public void highlightSong(List<Long> songId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void highlightUser(List<String> users) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void reset() {
		this.removeAll();
		tweets.clear();
		usrs.clear();
	}

	@Override
	public void resetBillBoard(String chartName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resetPhrase() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resetSong() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resetTime() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resetUser() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setBillBoard(String chartName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setPhrase(List<Long> phraseId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setSong(List<Long> songId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setTimeSpan(Timestamp start, Timestamp end) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setUser(List<String> users) {
		// TODO Auto-generated method stub
		
	}

}
